# encoding: utf-8
# @author: wangxin
# @software: garner
# @file: ttcmconstitutionidentificationViews.py
# @time: 2025/1/21 19:07
from django.views import View
import json
from tabnanny import check

# 导入Django核心模块中的序列化器，用于将Django模型转换为其他格式，如JSON、XML等
from django.core import serializers

# 导入Django序列化功能，以便在需要时将模型序列化
from django.core.serializers import serialize
from django.db.models import Q

# 导入HttpResponse和JsonResponse类，用于构建HTTP响应和JSON响应
from django.http import HttpResponse, JsonResponse

# 导入render函数，它用于根据请求和模板渲染网页
from django.shortcuts import render

from django.views import View
# 导入分页类
from django.core.paginator import Paginator
from backend.models import TTcmConstitutionIdentification



class TTcmConstitutionIdentificationView(View):
    def get(self, request, Page, PageSize):
        print(Page,PageSize)
        # “直接获取GET请求中'queryString'参数的值”

        queryString = request.GET.get("queryString")
        print(queryString)

        if (queryString == None):
            print("获取检查项列表")
            tTcmConstitutionIdentifications = TTcmConstitutionIdentification.objects.filter(is_show=1)
            # 实例化Paginator分页器，将检查项列表Check_Item_list按照PageSize指定的大小进行分页
            paginator = Paginator(tTcmConstitutionIdentifications, PageSize)
            # 获取指定页码的数据
            page_skus = paginator.page(Page)
            # 获取分页数据
            # total_page = paginator.num_pages
            # 获取总条数
            total_count = paginator.count
            # 将分页器中的对象列表赋值给checkItem_list变量
            # checkItem_list = paginator.object_list
            # 获取指定页码的数据化为JSON格式的字符串，然后将其反序列化为Python对象（列表）
            tTcmConstitutionIdentifications_Json_List = json.loads(serializers.serialize('json', page_skus))
            # print(Check_Item_Json_List)
            data_list = []
            for Check_Item_Json in tTcmConstitutionIdentifications_Json_List:


                data_josn = Check_Item_Json["fields"]
                tTcmConstitutionIdentification_id = Check_Item_Json['pk']  # 取检查项id
                data_josn["id"] =  tTcmConstitutionIdentification_id  # 将检查项id赋值给id字段
                # print(CheckItem_id)
                data_list.append(data_josn)
            result = {
                "status": "200",
                "message": "获取检查项成功",
                "total_count": total_count,
                "data": data_list

            }
            return JsonResponse(result)
        else:
            print("获取检查项列表")
            # 使用ORM方式，通过名称或代码中包含查询字符串来过滤检查项
            Check_Item_list = TTcmConstitutionIdentification.objects.filter(
                Q(is_show=1) & Q(constitution_name__contains=queryString) )
            # 实例化Paginator分页器，将检查项列表Check_Item_list按照PageSize指定的大小进行分页
            paginator = Paginator(Check_Item_list, PageSize)
            # 获取指定页码的数据
            page_skus = paginator.page(Page)
            # 获取分页数据
            # total_page = paginator.num_pages
            # 获取总条数
            total_count = paginator.count
            # 将分页器中的对象列表赋值给checkItem_list变量
            # checkItem_list = paginator.object_list
            # 获取指定页码的数据化为JSON格式的字符串，然后将其反序列化为Python对象（列表）
            tTcmConstitutionIdentifications_Json_List = json.loads(serializers.serialize('json', page_skus))
            # print(Check_Item_Json_List)
            data_list = []
            for Check_Item_Json in tTcmConstitutionIdentifications_Json_List:

                data_josn = Check_Item_Json["fields"]
                tTcmConstitutionIdentification_id = Check_Item_Json["pk"]
                data_josn["id"] = tTcmConstitutionIdentification_id  # 将检查项id赋值给id字段
                # print(CheckItem_id)
                data_list.append(data_josn)
            result = {
                "status": "200",
                "message": "获取检查项成功",
                "total_count": total_count,
                "data": data_list

            }
            return JsonResponse(result)

